import sys
import re

if (len(sys.argv) < 2):
    print("Usage: python opencv_tests_statics.py <log_path>")
    exit()


log_path = sys.argv[1]
with open(log_path, 'r') as f:
    while(True):
        line = f.readline()
        if not line:
            break
        if (line.startswith("Running ")):
            match = re.search(r'opencv_perf[^:]*', line)  # 匹配 "opencv_perf" 到 ":" 之间的内容
            bin_name = match.group(0)  # 提取匹配到的整个字符串
            test_case_num = 0
            ok_case = 0
            failed_case = 0
            total_ms = 0
        if (line.startswith("[ RUN      ] ")):
            test_case_num += 1
        if (line.startswith("[       OK ] ") and line.endswith(" ms)\n")):
            ok_case += 1
            match = re.search(r'\((\d+) ms\)', line)
            case_time = match.group(1)
            total_ms += int(case_time)
        if (line.startswith("[  FAILED  ] ") and line.endswith(" ms)\n")):
            failed_case += 1
            match = re.search(r'\((\d+) ms\)', line)
            case_time = match.group(1)
            total_ms += int(case_time)
        if (line.startswith("[==========] ") and line.endswith(" ms total)\n")):
            print(bin_name)
            print(f'[==========] {test_case_num} tests from  test cases ran. ({total_ms} ms total)')
            print(f'[  PASSED  ] {ok_case} tests.')
            if (failed_case > 0):
                print(f'[  FAILED  ] {failed_case} tests.')



'''
line = "[       OK ] Undistort.InitUndistortMap (34 ms)"
match = re.search(r'\((\d+) ms\)', line)
case_time = match.group(1)
print(case_time)
'''
        
